/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Anx.h"

#include "Anx_socket.h"
#include "Anx_tcp_select.h"

#include "lwip/tcp.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
static const char* TAG = "RTOS";


osThreadId Tcp_Select_Handle;
osThreadId LwipHandle;
extern struct tcp_pcb* client_pcb;

/* USER CODE END Variables */
osThreadId defaultTaskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void LwipTask(const void* argument);


/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* Hook prototypes */
void configureTimerForRunTimeStats(void);
unsigned long getRunTimeCounterValue(void);

/* USER CODE BEGIN 1 */
/* Functions needed when configGENERATE_RUN_TIME_STATS is on */
__weak void configureTimerForRunTimeStats(void)
{

}

__weak unsigned long getRunTimeCounterValue(void)
{
return 0;
}
/* USER CODE END 1 */

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
    Debug_log(TAG, "RTOS Init Start\r\n");
    Anx_UART_SendData(&huart1, "RTOS Init Start\r\n");

    Anx_Uart_Init(&huart1);
  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
            /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
            /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
            /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
            /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 512);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
            /* add threads, ... */
  osThreadDef(lwip, LwipTask, osPriorityHigh, 0, 256);
  LwipHandle = osThreadCreate(osThread(lwip), NULL);
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
  /* USER CODE BEGIN StartDefaultTask */

    osDelay(1000);

    uint32_t regvalue = 0;
    Debug_log(TAG, "F429IG LWIP LAN8720A %s\r\n", __TIME__);
    Debug_log(TAG, "addr %d \r\n", 0);
    HAL_ETH_ReadPHYRegister(&heth, 0, 0, &regvalue);
    Debug_log(TAG, "PHY_BCR %d: %X\r\n", 0, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 1, &regvalue);
    Debug_log(TAG, "PHY_BSR %d: %X\r\n", 1, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 2, &regvalue);
    Debug_log(TAG, "PHY 1 %d: %X\r\n", 2, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 3, &regvalue);
    Debug_log(TAG, "PHY2 %d: %X\r\n", 3, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 4, &regvalue);
    Debug_log(TAG, " %d: %X\r\n", 4, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 5, &regvalue);
    Debug_log(TAG, " %d: %X\r\n", 5, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 6, &regvalue);
    Debug_log(TAG, " %d: %X\r\n", 6, (unsigned int)regvalue);
    HAL_ETH_ReadPHYRegister(&heth, 0, 17, &regvalue);
    Debug_log(TAG, " %d: %04X\r\n", 17, (unsigned int)regvalue);

    // tcp_server_create(NULL,6060);
    
  osThreadDef(tcp_select, tcp_select_task, osPriorityHigh, 0, 512);
  Tcp_Select_Handle = osThreadCreate(osThread(tcp_select), NULL);
    /* Infinite loop */
    for (;;)
    {
        osDelay(500);
        HAL_GPIO_TogglePin(DS1_GPIO_Port, DS1_Pin);
    }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
char strTmp[500];
void LwipTask(const void* argument)
{
    // MX_LWIP_Init();
    for (;;)
    {
        osDelay(2000);
        HAL_GPIO_TogglePin(DS0_GPIO_Port, DS0_Pin);


        // taskENTER_CRITICAL();           /* 进入临界区 */
        // vTaskList(strTmp);
        // Debug_log("RTOS", "\r\n%s", strTmp);
        // memset(strTmp, 0, sizeof(strTmp));
        // taskEXIT_CRITICAL();            /* 退出临界区 */
    }
}

/* USER CODE END Application */
